﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Administration
{
    public partial class DeadlineManagement : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
             user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);
            
            if (!ReportHelper.IsAdmin(user.EmployeeId))
            {
                this.divError.InnerHtml = AlertMessage.NoAdminPermission;
                this.divConnect.Visible = false;
                this.divError.Visible = true;
            } 

            else if (!this.IsPostBack)
            {
                this.LoadData();
            }
        }

        protected void LoadData()
        {

            try
            {
                BEGDC.PreContract.BLL.IDeadline iDeadline = (BEGDC.PreContract.BLL.IDeadline)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Deadline_Service];
                BEGDC.PreContract.DAL.Deadline deadline = iDeadline.GetDeadline(1);
                if (deadline !=null)
                    this.DeadlineTextBox.Text = String.Format("{0:yyyy-MM-dd}",deadline.DeadlineForData);

            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void SureButton_Click(object sender, EventArgs e)
        {
               try
                {
                   BEGDC.PreContract.BLL.IDeadline iDeadline = (BEGDC.PreContract.BLL.IDeadline )BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Deadline_Service];
                   BEGDC.PreContract.DAL.Deadline deadline = iDeadline.GetDeadline(1);
                   
                   DateTime deadlineNow = DateTime.Parse(this.DeadlineTextBox.Text);
                   if (deadline != null)
                   {

                       if (iDeadline.UpdateDeadline(1, deadlineNow))
                       {
                           this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>UpdateDeadLine('" + this.DeadlineTextBox.Text  + "');alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.OperationSuccess) + "')</script>");
                       }
                   }
                   else
                   {

                       if (iDeadline.AddDeadline(deadlineNow)>0)
                       {
                           this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.OperationSuccess) + "')</script>");
                       }                   
                   }

                   BaseFunction.CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType.EditDeadline, string.Format("Deadline:{0}", deadlineNow.ToString("yyyy-MM-dd")), User.Identity.Name);
                }
                catch (Exception ex)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
                }
           
        }
    }
}
